<template>
  <div class="app-container">
    <git-navigation />
    <el-col :span="22" :offset="1">
      <el-row v-for="(v,index) in list" :key="index" class="group-bottom" :gutter="10">
        <el-col style="margin-left:-30px;" :span="11">
          <ul>
            <li class="li-msg" :title="v.message">
              <router-link :underline="false" :to="commitDetailUrl(v.id)">{{ v.message }}</router-link>
            </li>
            <li>
              <span
                class="commit-desc"
              >由 {{ v.committer_name }} 提交于 {{ v.committed_date | filterTime }}</span>
            </li>
          </ul>
        </el-col>
        <el-col :span="5" :offset="7">
          <el-button-group style="margin-right:-30px; margin-top:14px;float:right ">
            <el-button>
              <router-link :to="commitDetailUrl(v.id)">{{ v.short_id }}</router-link>
            </el-button>
            <el-button>
              <i
                v-clipboard:copy="v.id"
                v-clipboard:success="onCopy"
                v-clipboard:error="onError"
                class="el-icon-document-copy"
              />
            </el-button>
            <el-button @click="openFile(v.id)">
              <i class="el-icon-folder-opened" />
            </el-button>
          </el-button-group>
        </el-col>
      </el-row>
      <el-row style="margin-top:20px;">
        <el-button-group>
          <el-button
            type="primary"
            size="mini"
            :disabled="allowChangePage(-1)"
            @click="changePage(-1)"
          >上一页</el-button>
          <el-button
            type="primary"
            size="mini"
            :disabled="allowChangePage(1)"
            @click="changePage(1)"
          >下一页</el-button>
        </el-button-group>前往
        <el-input
          v-model="page"
          size="mini"
          min="1"
          style="width: 80px"
          autocomplete="off"
          @change="changePage(0)"
        />页
      </el-row>
    </el-col>
  </div>
</template>

<script>
import { postData } from '@/api/requestData';
import { parseTime } from '@/utils/index';
import GitNavigation from './components/GitNavigation';

export default {
  components: {
    GitNavigation
  },
  filters: {
    filterTime(time) {
      return parseTime(time, '{h}:{i}:{s}');
    }
  },
  data() {
    return {
      pid: this.$route.params.pid,
      pname: this.$route.params.projectName,
      iid: this.$route.params.iid,
      page: 1,
      listLoading: true,
      list: []
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    changePage(page) {
      if (page === -1) {
        this.page--;
      } else if (page === 1) {
        this.page++;
      }
      this.fetchData();
    },
    fetchData() {
      const pid = this.pid;
      const params = {
        page: this.page,
        iid: this.iid
      };
      console.log(params);
      postData('/git/public/merge/commits/list/' + pid, params)
        .then((response) => {
          this.listLoading = true;
          this.list = response.data;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = true;
        });
    },
    onCopy(e) {
      // 复制成功
      this.$message.success('复制成功');
    },
    onError(e) {
      // 复制失败
      this.$message('复制失败');
    },
    openFile(commitId) {
      const pid = this.$route.params.pid;
      const pname = this.$route.params.projectName;
      this.$router.push({
        path: '/git/code/' + pid + '/' + pname + '/' + commitId + '/root'
      });
    },
    commitDetailUrl(sha) {
      return '/git/commit/detail/' + this.pname + '/' + this.pid + '/' + sha;
    },
    allowChangePage(type) {
      const page = this.page;
      // 上一页
      if (type === -1 && page === 1) {
        return true;
      }
      if (type === 1 && this.list.length < 100) {
        return true;
      }
    }
  }
};
</script>

<style scoped>
.commit-bar {
  height: 40px;
  line-height: 40px;
  background-color: #fafafa;
  font-family: Microsoft YaHei, serif;
}

.group-bottom {
  border-bottom: 1px solid #f0f0f0;
}

.commit-desc {
  font-family: Microsoft YaHei, serif;
  font-size: small;
}

li {
  list-style: none;
  padding: 1px 1px;
}

.li-msg {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
</style>
